﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace PropertyManagement.login
{
    public partial class ValidateImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetImage();
        }

        public void GetImage()
        {
            string strcode = GenerateCheckCode();
            CreateCheckCodeImage(strcode);
            Session["ValidateImage"] = strcode;
        }

        public string GenerateCheckCode()
        {
            string checkCode = String.Empty;
            int number;
            string str = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            /*string str = "0,1,2,3,4,5,6,7,8,9," +
                "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z," +
                "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";*/
            string[] strResult = str.Split(new Char[] { ',' });
            string strReturn = "";

            Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < 4; i++)
            {
                Random rnd1 = new Random(rnd.Next() * unchecked((int)DateTime.Now.Ticks));
                number = rnd.Next(10);
                //number = rnd.Next(62);
                rnd = new Random(rnd.Next() * unchecked((int)DateTime.Now.Ticks));
                strReturn = strReturn + strResult[number].ToString();
            }
            return strReturn;
        }

        public void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
            {
                return;
            }
            Bitmap bmp = new Bitmap((int)Math.Ceiling(checkCode.Length * 12.5), 22);

            Graphics g = Graphics.FromImage(bmp);
            try
            {
                Random rdm = new Random();
                g.Clear(Color.White);
               /*int x1, x2, y1, y2;
                for (int i = 0; i < 2; i++)
                {
                    x1 = rdm.Next(bmp.Width);
                    x2 = rdm.Next(bmp.Width);
                    y1 = rdm.Next(bmp.Height);
                    y2 = rdm.Next(bmp.Height);

                    g.DrawLine(new Pen(Color.Black), x1, x2, y1, y2);
                }*/
                Font font = new Font("Aria", 12, FontStyle.Bold);
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                    new Rectangle(0, 0, bmp.Width, bmp.Height), Color.Blue, Color.Red, 1.2f, true);

                g.DrawString(checkCode, font, brush, 2, 2);

               /* for (int i = 0; i < 100; i++)
                {
                    x1 = rdm.Next(bmp.Width);
                    y1 = rdm.Next(bmp.Height);
                    bmp.SetPixel(x1, y1, Color.FromArgb(rdm.Next()));
                }*/
                g.DrawRectangle(new Pen(Color.SlateBlue), 0, 0, bmp.Width - 1, bmp.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            catch (Exception e)
            {

            }
            finally
            {
                g.Dispose();
                bmp.Dispose();
            }
        }
    }
}